OPC Studio User's Guide and Reference
Getting Started with OPC UA PubSub under .NET
Client and Subscriber Development > Getting Started > Getting Started under .NET Framework or .NET > Getting Started with OPC UA PubSub under .NET
In This Topic

In this Getting Started, we will use Visual Studio (on Windows) create a console application running that will subscribe to datasets produced by an OPC UA demo publisher, and display the received data on the console. You can target .NET Framework, or .NET 6, 7 or 8.

Note: The same code will work under Linux or macOS as well, but the steps to create the program are different - you will need to use .NET CLI Tools, or other IDE such as JetBrains Rider.

  1. Start Visual Studio.

  2. In this step, we will create a new .NET console application project.

    Select File -> New -> Project from the menu. The dialog(s) that follow differ in different versions of Visual Studio, so we wil just describe the intent here: Select Console App or Console App (.NET Framework), in C#. If given a choice of framework, make sure you select one of the supported .NET Runtimes. Finalize the dialog, creating the project.

    If you were not given a choice of framework, right-click on the project node in the Solution Explorer window, select Properties, and check the Target Framework setting.

  3. In this step, we will add a reference to the OpcLabs.QuickOpc NuGet package.

    Switch to the Solution Explorer window, right-click on the project node (ConsoleAppn), and select Manage NuGet Packages... command. In the NuGet: project window, switch to the Browse tab, and type OpcLabs.QuickOpc into the search box. Select the OpcLabs.QuickOpc package in the package list in the left pane of the window. In the right pane of the window, verify or change the package version next to the Version label. The version should be "Latest stable 5.80.build" (where build is a build number). Press the Install button.

    If you are developing for .NET Framework (not .NET) and have installed QuickOPC using its full Setup program, you can instead use assembly references to the QuickOPC assemblies installed locally. To do so, switch to the Solution Explorer window, right-click on the project node (ConsoleAppn), and select Add Reference... command. In the Reference Manager dialog, select Assemblies -> Extensions, check the boxes next to "OPC Labs Base Library" and "OPC Labs EasyOPC-UA Library", and press OK.
  4. Open the Program.cs file, and add following code to the beginning of the file:

        using OpcLabs.EasyOpc.UA.PubSub;
    
  5. In Program.cs, replace the body of the Main method by following code (use Copy&Paste from here):

         var subscriber = new EasyUASubscriber();
            subscriber.SubscribeDataSet("opc.udp://239.0.0.1", (_, eventArgs) =>
            {
                if (eventArgs.Succeeded)
                {
                    if (!(eventArgs.DataSetData is null))
                    {
                        Console.WriteLine();
                        Console.WriteLine($"Dataset data: {eventArgs.DataSetData}");
                        foreach (var pair in eventArgs.DataSetData.FieldDataDictionary)
                            Console.WriteLine(pair);
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine($"*** Failure: {eventArgs.ErrorMessageBrief}");
                }
            });
            Console.ReadLine();
            subscriber.UnsubscribeAllDataSets();
    
  6. Visit Tool Downloads page and download and unpack OPC UA Demo Publisher (the easiest to work with is Self-extracting EXE, but other format will do as well). In accordance with instructions on the UADemoPublisher Basics page, run the OPC UA Demo Publisher without further parameters.

    If you have installed QuickOPC using its full Setup program, you can bypass the tool download and installation described above, and simply run Tools -> OPC UA Demo Publisher from the Launcher application, or Windows Start menu.

    The OPC UA Demo Publisher will start broadcasting messages that our application will receive. You will see a progress indication (message counts) in its console window. You can run the OPC UA Demo Publisher either on the same computer where you are developing the application, or on a different computer on the same local network (IP subnet).

  7. Select Debug -> Start Debugging (F5) from the menu, or press the corresponding button on the toolbar.                   

    This will build and launch the program. The datasets from the publisher will be received by your subscriber application, and displayed on the console. Press Enter to exit the program.

Troubleshooting

See OPC UA PubSub Common Traps And Pitfalls if things do not work as expected - and specifically, if your subscriber application is not receiving anything and is giving you timeout errors.

Probably the most common issue is the necessity to properly specify a (non-default) network interface. For the OPC UA Demo Publisher, this is done using the --ConnectionNetworkInterface (-cni) option. For a suggestion about how this is done in your application, see e.g. the code comments in Examples - OPC UA PubSub - Callback using a lambda.

 

See Also